/**
 * @Author : andy.wangzhh
 * @Timestamp : 2016-06-10
 */
var app = sm("do_App");
var page = sm("do_Page");
var root = ui("$");
var hashdata = mm("do_HashData");
var common = require("common");
var nf = sm("do_Notification");
var http = require("helper/http");
var urlUser = require("url/user");
var userUtil = require("helper/user");
var imHelper = require("helper/im");
var push = require("helper/push");
var agreementFlag = true; //协议选中

page.on("back", function() {
    app.closePage()
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var headerNext = ui(addheader+".labelNext");
headerNext.visible = true;
headerNext.text = "注册";
var headerLeft = ui(addheader+".headerLeft");
var headerRight = ui(addheader+".headerRight");
headerLeft.on("touch", function() {
    app.closePage();
});
headerRight.on("touch", function() {
	common.open("source://view/user/loginCode.ui",{"title":"使用短信验证码注册"});
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "使用密码登录"
})
header.refreshData();

var loginBtn = ui("loginBtn");
var loginForm = ui("loginForm");
/*账号*/
var addphone = loginForm.add("phone", "source://view/reuse/textField.ui",0,0);
var phone = ui("phone");
phone.bindData(hashdata);
hashdata.addData({
    "textFieldHint" : "输入手机号",
    "textFieldPassword" : false,
    "textFieldInputType" : "PHONENUMBER"
});
phone.refreshData();
/*密码*/
var addpsw = loginForm.add("password", "source://view/reuse/textField.ui","phone");
var password = ui("password");
password.bindData(hashdata);
hashdata.addData({
	"textFieldPassword" : true,
	"textFieldInputType" : "ASC",
    "textFieldHint" : "请输入密码"
});
password.refreshData();



var addBtnLogin = loginBtn.add("btnLogin", "source://view/reuse/button.ui",0,0);
var btnLogin = ui("btnLogin");
btnLogin.bindData(hashdata);
hashdata.addData({
    "btnText" : "登录"
});
btnLogin.refreshData();

btnLogin.on("touch", "", 3000,function(data,e){
	page.hideKeyboard();//隐藏键盘
	var phoneValue = ui(addphone+".textField").text;
	var pwdValue = ui(addpsw+".textField").text;
	var tips = [];
	if(!agreementFlag){
		tips.push("请阅读并同意协议");
	}
	if(phoneValue.trim()===""){
		tips.push("手机号不能为空");
	}
	if(pwdValue.trim()===""){
		tips.push("密码不能为空");
	}
	if(tips.length!=0){
		nf.toast(tips.join("，"));
		return;
	}
	http.post(urlUser.url.login.doLoginWithPhonePwd,{"phone":phoneValue,"password":pwdValue,"registrationId":push.getRegistrationID()},function(data){
		userUtil.loginToken(data.loginUser.token);
		userUtil.userId(data.loginUser.id);
		userUtil.userLogo(data.logo);
		if(data.hasIdentityAuth && data.hasIdentityAuth!=null && data.hasIdentityAuth!=="undefined"){
			userUtil.hasIdentityAuth(data.hasIdentityAuth);
		}
		if(data.hasCommunityAuth && data.hasCommunityAuth!=null && data.hasCommunityAuth!=="undefined"){
			userUtil.hasCommunityAuth(data.hasCommunityAuth);
		}
		push.handleAudience();
		app.fire("fireUserUpdate",{"logo":data.logo,"nick":data.nick,"hasIdentityAuth":data.hasIdentityAuth});
		imHelper.initIM();
		imHelper.loginIM(function(){
			nf.toast("登录成功");
			app.closePageToID({fromPage:"login"},"","qquan_page_index");
		});
	});
});

var btnLoginCode = ui("btnLoginCode");
btnLoginCode.on("touch", function(data,e){
	common.open("source://view/user/loginCode.ui");
});
page.on("result", function(data) {
	if(data&&data!=null&&data!=="undefined"&&data.isLogin==="1"){
		app.closePage(data);
	}
});

//圈圈生活协议
var agreementCheck = ui("agreementCheck");
var agreementCheckImg = ui("agreementCheckImg");
var agreeImg = ["source://image/common/checkboxcheck.png","source://image/common/checkbox.png"];
agreementCheck.on("checkChanged", function(data, e) {
	agreementFlag = this.checked;
	agreementCheckImg.source = agreementFlag?agreeImg[0]:agreeImg[1];
});
var agreementCheckLayout = ui("agreementCheckLayout");
agreementCheckLayout.on("touch", "", 300,function(data,e){
	common.open("source://view/user/agreement.ui");
});
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})